<?php

namespace app\model;

use think\Model;
use think\model\relation\HasMany;

class Product extends Model
{
    public function getPage(): array
    {
        $param = request()->get();
        $page = $param['page'] ?? 1;
        $limit = $param['limit'] ?? 50;
        $data = $this->with(['attr'])->when(!empty($param['name']), function ($query) use ($param) {
            $query->whereLike('name', '%' . $param['name'] . '%');
        })->when(!empty($param['state']), function ($query) use ($param) {
            $query->where('state', $param['state']);
        })->when(!empty($param['type']), function ($query) use ($param) {
            $query->where('type', $param['type']);
        })->when(!empty($param['brand']), function ($query) use ($param) {
            $query->whereLike('brand', "%{$param['brand']}%");
        })->where(['is_del' => 1])->hidden(['content', 'article', 'detail', 'account'])->order(['create_time' => 'desc'])->paginate(['list_rows' => $limit, 'page' => $page])->toArray();
        return ['total' => $data['total'], 'lst' => $data['data']];
    }

    public function attr(): HasMany
    {
        return $this->hasMany(ProductAttr::class, 'pid', 'id');
    }

    public function updateOrAdd(): array
    {
        $param = request()->post();
        $attr = $param['attr'];
        unset($param['attr'], $param['create_time'], $param['update_time']);
        $param['account'] = json_encode($param['account'], JSON_UNESCAPED_UNICODE);
        $param['detail'] = htmlspecialchars($param['detail']);
        $param['content'] = htmlspecialchars($param['content']);
        $param['article'] = htmlspecialchars($param['article']);
        $this->startTrans();
        $this->exists(!empty($param['id']))->save($param);
        $attr_id = array_column($attr, "id");
        $attr_id = array_values(array_filter($attr_id));
        $attrModel = app(ProductAttr::class);
        $uni_attr_id = $attrModel->where(['pid' => $this->id])->column("id");
        $del_id = array_diff($uni_attr_id, $attr_id);//要删除的规格
        $insert = $update = [];
        foreach ($attr as $v) {
            $v['pid'] = $this->id;
            if (empty($v['id'])) {
                $insert[] = $v;
            } else {
                $update[] = $v;
            }
        }

        if (!empty($insert)) $attrModel->exists(false)->saveAll($insert);
        if (!empty($update)) $attrModel->exists()->saveAll($update);
        if (!empty($del_id)) $attrModel->where(['id' => $del_id])->delete();
        $this->commit();
        return $this->toArray();
    }

    public function detail()
    {
        $id = request()->param("id");
        $data = $this->with(['attr' => function ($query) {
            $query->hidden(['create_time', 'update_time']);
        }])->findOrFail($id);
        $data->account = json_decode($data->account, true);
        $data->content = htmlspecialchars_decode($data->content);
        $data->detail = htmlspecialchars_decode($data->detail);
        $data->article = htmlspecialchars_decode($data->article);
        return $data;
    }

    public function delProduct(): array
    {
        $id = request()->param("id");
        $this->where(['id' => $id])->update(['is_del' => 0]);
        return $this->toArray();
    }

    public function brandDetail(): array
    {
        $brand = request()->param("brand");
        $data = $this->with(['attr' => function ($query) {
            $query->order("face_value");
        }])->where('brand', $brand)->findOrFail();
        $data = $data->toArray();
        $data['content'] = htmlspecialchars_decode($data['content']);
        $data['detail'] = htmlspecialchars_decode($data['detail']);
        $data['article'] = htmlspecialchars_decode($data['article']);
        $data['account'] = json_decode($data['account'], true);
        return $data;
    }

    public function searchProduct(): array
    {
        return $this->where(['is_del' => 1])->column("id,name");
    }
}
